package com.hero.take.entity.result;

import lombok.Data;

@Data
public class R<T> {

    private Integer code;
    private String message;
    private T data;
    private boolean success;

    private R(T data, String message, Integer code, boolean success) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.success = success;
    }

    public static <T> R<T> success(T data, String message) {
        return new R<>(data, message, 200, true);
    }

    public static <T> R<T> success(T data) {
        return success(data, "成功");
    }

    public static <T> R<T> success() {
        return new R<>(null, "成功", 200, true);
    }

    public static <T> R<T> success(String message) {
        return new R<>(null, message, 200, true);
    }


    public static <T> R<T> error(String message, Integer code) {
        return new R<>(null, message, code, false);
    }

    public static <T> R<T> error(String message) {
        return error(message, 450);
    }

    public static <T> R<T> error(String message, String... infos) {
        return error(String.format(message, infos));
    }

    public static <T> R<T> error(ResponseCode code) {
        return error(code.getMessage(), code.getCode());
    }

    public R<T> setMessage(String message) {
        this.message = message;
        return this;
    }
}